package cn.sdhuijin.vms.sms.parser;

import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import cn.sdhuijin.vms.model.CarRecord;
import cn.sdhuijin.vms.model.PersonRecord;

public class Util {
	private static SimpleDateFormat format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static Date fromString(String time) throws ParseException {
		return format.parse(time);
	}

	public static String fromDate(Date time) {
		return format.format(time);
	}

	public static void exportPersonRecordExcelDocument(List<PersonRecord> list,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		HSSFWorkbook workbook = new HSSFWorkbook();
		response.setContentType("application/octet-stream");
		response.setHeader(
				"Content-Disposition",
				"attachment; filename=\""
						+ URLEncoder.encode("人员出入记录统计" + df.format(new Date())
								+ ".xls", "UTF-8") + "\"");

		HSSFSheet sheet = workbook.createSheet("人员出入记录统计"
				+ df.format(new Date()));
		HSSFRow header = sheet.createRow(0);

		header.createCell((short) 0).setCellValue("姓名");
		header.createCell((short) 1).setCellValue("类型");
		header.createCell((short) 2).setCellValue("出入情况");
		header.createCell((short) 3).setCellValue("时间");
		header.createCell((short) 4).setCellValue("申请事由");
		HSSFCellStyle style2 = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style2.setFont(font);
		header.getCell((short) 0).setCellStyle(style2);
		header.getCell((short) 1).setCellStyle(style2);
		header.getCell((short) 2).setCellStyle(style2);
		header.getCell((short) 3).setCellStyle(style2);
		header.getCell((short) 4).setCellStyle(style2);
		// 填充数据
		int rowNum = 1;
		for (Iterator<PersonRecord> iter = list.iterator(); iter.hasNext();) {
			PersonRecord personRecord = (PersonRecord) iter.next();
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell((short) 0).setCellValue(
					personRecord.getPerson().getName());
			row.createCell((short) 1).setCellValue(personRecord.getType());
			row.createCell((short) 2).setCellValue(personRecord.getWarn());
			row.createCell((short) 3).setCellValue(
					Util.fromDate(personRecord.getDate()));
			if (personRecord.getLeave() == null) {
				row.createCell((short) 4).setCellValue("未填写");
			} else {
				row.createCell((short) 4).setCellValue(
						personRecord.getLeave().getReason());
			}

		}
		workbook.write(response.getOutputStream());
	}

	public static void exportCarRecordExcelDocument(List<CarRecord> list,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		HSSFWorkbook workbook = new HSSFWorkbook();
		response.setContentType("application/octet-stream");
		response.setHeader(
				"Content-Disposition",
				"attachment; filename=\""
						+ URLEncoder.encode("车辆出入记录统计" + df.format(new Date())
								+ ".xls", "UTF-8") + "\"");

		HSSFSheet sheet = workbook.createSheet("车辆出入记录统计"
				+ df.format(new Date()));
		HSSFRow header = sheet.createRow(0);

		header.createCell((short) 0).setCellValue("车牌号");
		header.createCell((short) 1).setCellValue("类型");
		header.createCell((short) 2).setCellValue("时间");
		header.createCell((short) 3).setCellValue("车主姓名");
		header.createCell((short) 4).setCellValue("车主手机");
		HSSFCellStyle style2 = workbook.createCellStyle();
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style2.setFont(font);
		header.getCell((short) 0).setCellStyle(style2);
		header.getCell((short) 1).setCellStyle(style2);
		header.getCell((short) 2).setCellStyle(style2);
		header.getCell((short) 3).setCellStyle(style2);
		header.getCell((short) 4).setCellStyle(style2);
		// 填充数据
		int rowNum = 1;
		for (Iterator<CarRecord> iter = list.iterator(); iter.hasNext();) {
			CarRecord carRecord = (CarRecord) iter.next();
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell((short) 0).setCellValue(
					carRecord.getCar().getLicense());
			row.createCell((short) 1).setCellValue(carRecord.getType());
			row.createCell((short) 2).setCellValue(
					Util.fromDate(carRecord.getDate()));
			row.createCell((short) 3).setCellValue(
					carRecord.getCar().getMasterName());
			row.createCell((short) 4).setCellValue(
					carRecord.getCar().getMasterPhone());
		}
		workbook.write(response.getOutputStream());
	}
}
